package com.asset.dispoal.business.bsAssetInstitutionBatch.service.commom;

import com.asset.dispoal.business.bsAssetInstitutionBatch.domain.db.entity.BsAssetInstitutionBatch;
import com.asset.dispoal.business.bsAssetInstitutionBatch.domain.db.mapper.BsAssetInstitutionBatchMapper;
import com.asset.dispoal.business.bsAssetInstitutionBatch.dto.BsAssetInstitutionBatchRequest;
import com.asset.dispoal.business.bsAssetInstitutionBatch.service.converter.BsAssetInstitutionBatchConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.util.CollectionUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.util.List;

/**
 * @author qr
 * @date 2022/3/10$
 */
@Component
public class BsAssetInstitutionBatchUpdate implements BaseCommand<BsAssetInstitutionBatchRequest, Result> {

    @Autowired
    BsAssetInstitutionBatchMapper bsAssetInstitutionBatchMapper;

    @Override
    public Result execute(BsAssetInstitutionBatchRequest bsAssetInstitutionBatchRequest) {
        BsAssetInstitutionBatch bsAssetInstitutionBatch = BsAssetInstitutionBatchConverter.INSTANCE.resToDo(bsAssetInstitutionBatchRequest);
        final List<BsAssetInstitutionBatch> bsAssetInstitutionBatches = bsAssetInstitutionBatchMapper.check(bsAssetInstitutionBatch);
        if ((CollectionUtil.isNotEmpty(bsAssetInstitutionBatches) ||
                bsAssetInstitutionBatches.size() > 1) && !bsAssetInstitutionBatches.get(0).getId().equals(bsAssetInstitutionBatchRequest.getId())) {
            return Result.faild("批次编号/名称不能重复");
        }

        bsAssetInstitutionBatchMapper.update(bsAssetInstitutionBatch);
        return Result.ok();
    }
}
